/*
 ******************
 * author: 马丰彦
 * date: 2019
 * function: 权限对照表
 ******************
 */
const power = [{
  title: '用户管理',
  icon: 'users',
  code: 'usercenter',
  btns: [{
    btnName: '全网用户汇总',
    btnCode: 'search'
  }],
  head: '在网用户总量',
  info: '500'
}, {
  title: '业务管理',
  icon: 'briefcase',
  code: 'business',
  btns: [{
    btnName: '产品管理',
    btnCode: 'product'
  }, {
    btnName: '订单列表',
    btnCode: 'accountTable'
  }, {
    btnName: '计费策略管理',
    btnCode: 'tactics'
  }],
  head: '线上产品数量',
  info: '87'
}, {
  title: '账务管理',
  icon: 'accountbook',
  code: 'accountcenter',
  btns: [{
    btnName: '收支明细',
    btnCode: 'income'
  }, {
    btnName: 'CP收入',
    btnCode: 'cp'
  }, {
    btnName: '产品收入',
    btnCode: 'product'
  }],
  head: '本月订单总额',
  info: '200'
}, /* , {
  title: '合作伙伴管理',
  icon: 'hezuo',
  code: 'partner',
  btns: [{
    btnName: 'CP管理',
    btnCode: 'cp'
  }, {
    btnName: 'SP管理',
    btnCode: 'sp'
  }],
  head: 'cp数量',
  info: '301'
} */{
  title: '会员管理',
  icon: 'attestation',
  code: 'integral',
  btns: [{
    btnName: '会员等级管理',
    btnCode: 'vip'
  }, {
    btnName: '积分触发管理',
    btnCode: 'point'
  }, {
    btnName: '积分成长值流水',
    btnCode: 'integralLogs'
  }],
  head: '',
  info: '0'
}, {
  title: '系统设置',
  icon: 'setting2',
  code: 'setting',
  btns: [{
    btnName: '角色管理',
    btnCode: 'role'
  }, {
    btnName: '账号管理',
    btnCode: 'account'
  }, {
    btnName: '操作日志',
    btnCode: 'log'
  }],
  head: '当前操作账号数',
  info: '12'
}, {
  title: '数据监控',
  icon: 'ratings-static1',
  code: 'datamonit',
  btns: [{
    btnName: '用户数据监控',
    btnCode: 'account'
  }, {
    btnName: '产品数据监控',
    btnCode: 'product'
  }],
  head: '同步数据异常条数',
  info: '10'
}]
// 权限视图层展示
export function showPower(inputPower) {
  var status = Array.isArray(inputPower)
  // 定义筛选出的权限
  var menuPower = []
  var btnPower = []
  if (status && inputPower.length > 0) {
    // 查找分系统字段权限
    inputPower.forEach(item => {
      // 筛查menu
      if (item.powerType === 'menu') {
        const powerCode = item.powerCode
        const len = powerCode.indexOf('-')
        if (len !== -1) {
          // system power
          const result = powerCode.substring(0, len)
          // btn power
          const lastLen = powerCode.substring(len + 1, powerCode.length)
          // 去重 + 处理system权限
          if (menuPower.indexOf(result) === -1) {
            menuPower.push(result)
          }
          // 处理btn权限(防止不同system会有重名的btn可能，杜绝掉)
          btnPower.push(lastLen)
        }
      }
    })
    return findPower(menuPower, btnPower, inputPower)
  }
}
// 比对system
function findPower(apiPower, btnPower, inputPower) {
  // 定义应该显示的系统界面
  var menuShow = []
  if (apiPower.length > 0) {
    power.forEach(item => {
      // btn比对
      const btnarrs = []
      if (apiPower.indexOf(item.code) !== -1) {
        item.btns.forEach(arr => {
          if (btnPower.indexOf(arr.btnCode) !== -1) {
            btnarrs.push(arr)
          }
        })
        item.btns = btnarrs
        menuShow.push(item)
      }
    })
  }
  // 特殊情况：用户中心只有分平台权限的话就得手动把分平台的用户中心给加上了
  // 今天是2019-11-06,晴，偏冷
  // 我在用我的IPHONE XR 128G 玫瑰红 连着 BOSE QC35|| 耳机，听着<<NEXT TO YOU>>
  /* console.log(apiPower)
  console.log(inputPower) */
  console.log(menuShow)
  var middleWare = false
  for (let i = 0; i < menuShow.length; i++) {
    if (menuShow[i].code === 'usercenter') {
      middleWare = true
      break
    }
  }

  if (!middleWare) {
    inputPower.forEach(item => {
      if (item.powerCode === 'usercenter' && item.powerType === 'system') {
        menuShow.unshift({
          btns: [],
          title: '用户管理',
          code: 'usercenter',
          icon: 'users',
          head: '在网用户总量',
          info: '500'
        })
      }
    })
  }
  return menuShow
}
// 比对btn
/* function findBtn(code, temp, btnPower) {
  let btnResult = []
  if (btnPower.length > 0) {
    btnPower.forEach(item => {
      for (var key in item) {
        if (key === code) {
          temp.forEach(value => {
            if (value.btnCode === item[key])
            btnResult.push(item[key])
          })
        }
      }
    })
  }
  return btnResult
} */
